<?php
class BuildExamStructModel extends Model
{
    function  __construct()
    {
        parent::__construct();        
    }

    public function getSubjectList($id=null, $name=null)
    {
        $rs=null;
        $this->loadTable('subjects');
        $subjectTable = new SubjectsTable();
        if ($id==null && $name==null)
            $rs = $subjectTable->get();
        else{
            $condition = array('id'=>array('like','%'.$id.'%'), 'name'=>array('like','%'.$name.'%'));
            $rs = $subjectTable->get($condition);
        }
        return $rs;
    }
    
    public function save($subjectID, $allQuestionCount, $time, $examQuestionCount, $weight1, $weight2, $weight3, $weight4, $qc, $sc, $sid)
    {
        // validation
        $check = true;
        $message = '';
        if ($examQuestionCount > $allQuestionCount){
            $check = false;
            $message .= '<br /> - Không đủ câu hỏi trong ngân hàng đề.';
        }
        if (($weight1 + $weight2 + $weight3 + $weight4) != 100){
            $check = false;
            $message .= '<br /> - Tỉ lệ độ khó trong bài phải có tổng bằng 100%.';
        }
        if (array_sum($qc) != $examQuestionCount){
            $check = false;
            $message .= '<br /> - Tổng số lượng câu hỏi trong các chương không đúng với số câu hỏi trong bài thi.';
        }
        $n = count($qc);
        for ($i=0; $i<$n; $i++)
            if ($qc[$i] > $sc[$i]){
                $check = false;
                $message .= '<br /> - Không đủ câu hỏi trong một chương nào đó.';
                break;
            }
        
        // redirect    
        if (!$check){
            $message = 'Có lỗi xảy ra. Hãy kiểm tra lại!' . $message;
        }
        else{
            //update to database
            //insert or update examstructures
            $this->loadTable('examstructures');
            if ($this->checkExamStructureExists($subjectID)){
                $examStructData = array('time'=>$time, 'questioncount'=>$examQuestionCount, 'weight1'=>$weight1, 'weight2'=>$weight2, 'weight3'=>$weight3, 'weight4'=>$weight4);
                $examStructObj = new ExamStructuresTable($examStructData);
                $examStructObj->update(array('subjectid'=>$subjectID));
            }
            else{
                $examStructData = array('subjectid'=>$subjectID, 'time'=>$time, 'questioncount'=>$examQuestionCount, 'weight1'=>$weight1, 'weight2'=>$weight2, 'weight3'=>$weight3, 'weight4'=>$weight4);
                $examStructObj = new ExamStructuresTable($examStructData);
                $examStructObj->insert(false);
            }
            
            //update sections weight
            $this->loadTable('sections');
            $n = count($qc);
            for ($i=0; $i<$n; $i++)
            {
                $sectionObj = new SectionsTable(array('weight'=>$qc[$i]));
                $condition = array('id'=>$sid[$i]);
                $sectionObj->update($condition);
            }
            
            $message = "Xây dựng cấu trúc đề thi cho môn học $subjectID thành công!";
        }

        return array('check'=>$check, 'message'=>$message);
    }
    
    public function getQuestionCount($subjectID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("SELECT COUNT(*) FROM questions AS q JOIN sections as s ON q.sectionid = s.id JOIN subjects AS sj ON s.subjectid = sj.id WHERE sj.id = '$subjectID'");
        while ($row = mysql_fetch_array($rs)) $questionCount = $row[0];
        return $questionCount;
    }
    
    public function getSubject($subjectID)
    {
        $this->loadTable('subjects');
        $subjectTable = new SubjectsTable();
        $condition = array('id'=>$subjectID);
        $subjects = $subjectTable->get($condition)->getAll();
        return $subjects[0];
    }
    
    public function getSectionList($subjectID)
    {
        $db = DataProvider::getInstance();
        $rs = $db->query("SELECT s.id, s.name, COUNT(q.sectionid) as count FROM sections as s JOIN subjects AS sj ON s.subjectid = sj.id LEFT JOIN questions as q ON s.id = q.sectionid WHERE sj.id = '$subjectID' GROUP BY s.id");
        $objArr = array();
        while ($row = mysql_fetch_array($rs))
        {
            $objArr[] = new DataObject($row);
        }
        return $objArr;
    }
    
    public function checkExamStructureExists($subjectID)
    {
        $this->loadTable('examstructures');
        $examStructureTable = new ExamStructuresTable();
        $condition = array('subjectid'=>$subjectID);
        $examStructures = $examStructureTable->get($condition)->getAll();
        if (count($examStructures) > 0) return true;
        else return false;
    }
}
?>
